import React, { useEffect, useState } from 'react';
import {
    Button,
    DatePicker,
    Form,
    Input,
    InputNumber,
    message,
    Select,
} from 'antd';
import * as API from '../../axios/http'
function myform(props:any) {
    const [form] = Form.useForm();
    useEffect(() => {
        // 判断props中的title属性是否为"编辑"
        if (props.title == "编辑") {
            // 如果是，则将props中的data属性赋值给表单
            console.log(props);

            // form.setFieldsValue(props.itemobj);
            const itemobjs = {
                id: props.itemobj.id,
                key: props.itemobj.key,
                name: props.itemobj.name,
                Customer: props.itemobj.Customer,
                stage: props.itemobj.stage,
                bjnum: props.itemobj.bjnum,
                price: props.itemobj.price,
                time: props.itemobj.time,
                days: props.itemobj.days
            }
            console.log(form);

            form.setFieldsValue(itemobjs);
        }
    })
    // 定义一个函数，用于处理表单提交事件
    const onFinish = (values: any) => {
        // 判断props中的title属性是否为"添加"
        if (props.title == "添加") {
            // 生成一个唯一的id
            const idd = new Date().getTime()
            const obj = {
                id: idd,
                key: JSON.stringify(idd),
                ...values,
                time: new Date(values.time).getTime(),//将时间戳转换为时间
            }
            console.log(obj);
            const res = API.http_addZzt(obj).then(() => {
                props.setdata(obj)
            })
            message.success('添加成功')
        }else{
            const obj = {
                id: props.itemobj.id,
                key: props.itemobj.key,
                ...values
            }
            const res = API.http_editZzt(obj).then(() => {
                props.setdata(obj)
            })
            message.success('编辑成功')
        }
        props.handleOk()
    };

    return (
        <Form
            onFinish={onFinish}
            form={form}
            style={{ maxWidth: 600 }}
        >
            <Form.Item label="商机标题" name="name" rules={[{ required: true, message: '请输入商机标题!' }]}>
                <Input placeholder='请输入商机标题' />
            </Form.Item>
            <Form.Item label="关联客户" name="Customer" rules={[{ required: true, message: '请选择关联客户!' }]}>
                <Select placeholder="请选择关联客户">
                    <Select.Option value="深圳市亿恩科技有限公司">深圳市亿恩科技有限公司</Select.Option>
                    <Select.Option value="广州市云启智联科技有限公司">广州市云启智联科技有限公司</Select.Option>
                    <Select.Option value="上海市星耀智研科技有限公司">上海市星耀智研科技有限公司</Select.Option>
                    <Select.Option value="成都市鸿途智汇科技有限公司">成都市鸿途智汇科技有限公司</Select.Option>
                </Select>
            </Form.Item>
            <Form.Item label="商机阶段" name="stage" rules={[{ required: true, message: '请选择商机阶段!' }]}>
                <Select placeholder="请选择商机阶段">
                    <Select.Option value="初步洽谈">初步洽谈</Select.Option>
                    <Select.Option value="需求调研">需求调研</Select.Option>
                </Select>
            </Form.Item>
            <Form.Item label="报价记录" name="bjnum" rules={[{ required: true, message: '请输入报价记录!' }]}>
                <InputNumber min={1} placeholder='报价记录' />
            </Form.Item>
            <Form.Item label="报价总金额" name="price" rules={[{ required: true, message: '请输入报价总金额!' }]}>
                <InputNumber min={1} placeholder='报价总金额' />
            </Form.Item>
            {/* <Form.Item label="最后跟进" name="time" rules={[{ required: true, message: '请选择最后跟进时间!' }]}>
                <DatePicker placeholder='最后跟进时间' />
            </Form.Item> */}
            <Form.Item label="未跟进天数" name="days" rules={[{ required: true, message: '请输入未跟进天数!' }]}>
                <InputNumber min={1} placeholder='未跟进天数' />
            </Form.Item>
            <Form.Item label="">
                <Button type='primary' htmlType="submit">确定</Button>
            </Form.Item>
        </Form>
    );
};

export default myform;